<?php

namespace app\adminapi\controller;

use app\adminapi\dto\sysuser\LoginReqVO;
use app\adminapi\dto\sysuser\ModifyAavtarReqVO;
use app\adminapi\dto\sysuser\ModifyPwdReqVO;
use app\adminapi\service\SysMenuService;
use app\adminapi\service\SysUserService;
use app\adminapi\validate\LoginValidate;
use app\adminapi\validate\ModifyAvatarValidate;
use app\adminapi\validate\ModifyPwdValidate;
use app\common\cache\AdminTokenCache;
use app\common\controller\BaseAdminController;
use think\response\Json;

class LoginController extends BaseAdminController
{
    public array $notNeedLogin = ['index'];

    public function index(): Json
    {
        $params = (new LoginValidate())->post()->goCheck('login');
        $result = (new SysUserService())->login($this->convert($params, LoginReqVO::class));
        return app('json')->successWithData($result);
    }

    public function logout(): Json
    {
        (new AdminTokenCache())->clearAdminToken($this->adminToken);
        return app('json')->success();
    }

    public function getInfo(): Json
    {
        $result = [
            'menus'=>(new SysMenuService())->getTreeSysMenu(),
            'userInfo'=>(new SysUserService())->getSysUser($this->adminId),
            'permissions'=>(new SysMenuService())->getUserPermissions($this->adminId)
        ];
        return app('json')->successWithData($result);
    }

    public function modifyPwd(): Json
    {
        $params = (new ModifyPwdValidate())->put()->goCheck();
        $result = (new SysUserService())->modifyPwd($this->convert($params,ModifyPwdReqVO::class));
        return app('json')->toAjax($result);
    }

    public function modifyAvatar()
    {
        $params = (new ModifyAvatarValidate())->put()->goCheck();
        $result = (new SysUserService())->modifyAvatar($this->convert($params, ModifyAavtarReqVO::class));
        return app('json')->successWithData($result);
    }
}